import { parse } from 'url';
import webpath from '@/utils/webpath';

// mock tableListDataSource
let tableListDataSource = [];
for (let i = 0; i < 46; i += 1) {
    tableListDataSource.push({
        system_id: `李海波 system_id ${i}`,
        auth_id: `李海波 auth_id ${i}`,
        group_id: Math.floor(Math.random() * 10) % 4,
    });
}
let userListDataSource = [];
for (let i = 0; i < 46; i += 1) {
    userListDataSource.push({
        auth_id: `auth ${i}`,
        system_id: `姜旭聪 group_id ${i}`,
        group_id: Math.floor(Math.random() * 10) % 4,
    });
}

let treeNodeArray = [
'0','1','2','3'
];
let treeNode = 3;
function getSelectedProSystemByAuthId (req,res,u,b) {
	treeNode += 1;
	treeNodeArray = [...treeNodeArray,treeNode.toString()];
	let result = {
        list: treeNodeArray,
    };
	return res.json(result);
}

let mockData = [];
for (let i = 0; i < 20; i++) {
  mockData.push({
    key: i.toString(),
    name: `content${i + 1}`,
    ename: `description of content${i + 1}`,
    version: i % 4 === 0,
    system_id: `system${i}`
  });
}

function queryPageForProSystemChild(req,res,u) {
	getChild(req, res, u)
}

function getChild(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = mockData;

  if (params.sorter!=undefined) {
      const s = params.sorter.split('_');
      dataSource = dataSource.sort((prev, next) => {
          if (s[1] === 'descend') {
          return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
  });
  }

  if (params.status!=undefined) {
  	const status = params.status.split(',');
  let filterDataSource = [];
  status.forEach(s => {
  filterDataSource = filterDataSource.concat(
  dataSource.filter(data =>
  {

      if(data.status!=undefined){
      return parseInt(data.status, 10) === parseInt(s[0], 10);
      }
  })
  );
  });
  dataSource = filterDataSource;
  }

  if (params.name!=undefined) {
  dataSource = dataSource.filter(data => {

      if(data.name!=undefined){
      return data.name.indexOf(params.name) > -1;
      }

  });
  }

  let pageSize = 10;
  if (params.pageSize!=undefined) {
      pageSize = params.pageSize * 1;
  }

  const result = {
      list: dataSource,
      pagination: {
          total: dataSource.length,
          pageSize,
          current: parseInt(params.currentPage, 10) || 1,
      },
  };

  return res.json(result);
}

function getAuth(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = userListDataSource;

  if (params.sorter!=undefined) {
      const s = params.sorter.split('_');
      dataSource = dataSource.sort((prev, next) => {
          if (s[1] === 'descend') {
          return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
  });
  }

  if (params.status!=undefined) {
  	const status = params.status.split(',');
  let filterDataSource = [];
  status.forEach(s => {
  filterDataSource = filterDataSource.concat(
  dataSource.filter(data =>
  {

      if(data.status!=undefined){
      return parseInt(data.status, 10) === parseInt(s[0], 10);
      }
  })
  );
  });
  dataSource = filterDataSource;
  }

  if (params.name!=undefined) {
  dataSource = dataSource.filter(data => {

      if(data.name!=undefined){
      return data.name.indexOf(params.name) > -1;
      }

  });
  }

  let pageSize = 10;
  if (params.pageSize!=undefined) {
      pageSize = params.pageSize * 1;
  }

  const result = {
      list: dataSource,
      pagination: {
          total: dataSource.length,
          pageSize,
          current: parseInt(params.currentPage, 10) || 1,
      },
  };

  return res.json(result);
}

function getAllAuth (req,res,u,b){
	let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    userListDataSource = userListDataSource.filter(item => id.indexOf(item.id) === -1);
	return getAuth(req, res, u)
}
function getPageProSystemByAuthId(req, res, u, b) {
    return getAuth(req, res, u);
}
function getProSystem(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = tableListDataSource;

    if (params.sorter!=undefined) {
        const s = params.sorter.split('_');
        dataSource = dataSource.sort((prev, next) => {
            if (s[1] === 'descend') {
            return next[s[0]] - prev[s[0]];
        }
        return prev[s[0]] - next[s[0]];
    });
    }

    if (params.status!=undefined) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
    filterDataSource = filterDataSource.concat(
    dataSource.filter(data =>
    {

        if(data.status!=undefined){
        return parseInt(data.status, 10) === parseInt(s[0], 10);
        }
    })
    );
    });
    dataSource = filterDataSource;
    }

    if (params.name!=undefined) {
    dataSource = dataSource.filter(data => {

        if(data.name!=undefined){
        return data.name.indexOf(params.name) > -1;
        }

    });
    }

    let pageSize = 10;
    if (params.pageSize!=undefined) {
        pageSize = params.pageSize * 1;
    }

    const result = {
        list: dataSource,
        pagination: {
            total: dataSource.length,
            pageSize,
            current: parseInt(params.currentPage, 10) || 1,
        },
    };

    return res.json(result);
}

function getProSystemAuth(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = tableListDataSource;

    if (params.sorter!=undefined) {
        const s = params.sorter.split('_');
        dataSource = dataSource.sort((prev, next) => {
            if (s[1] === 'descend') {
            return next[s[0]] - prev[s[0]];
        }
        return prev[s[0]] - next[s[0]];
    });
    }

    if (params.status!=undefined) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
    filterDataSource = filterDataSource.concat(
    dataSource.filter(data =>
    {

        if(data.status!=undefined){
        return parseInt(data.status, 10) === parseInt(s[0], 10);
        }
    })
    );
    });
    dataSource = filterDataSource;
    }

    if (params.name!=undefined) {
    dataSource = dataSource.filter(data => {

        if(data.name!=undefined){
        return data.name.indexOf(params.name) > -1;
        }

    });
    }

    let pageSize = 10;
    if (params.pageSize!=undefined) {
        pageSize = params.pageSize * 1;
    }

    const result = {
        list: dataSource,
        pagination: {
            total: dataSource.length,
            pageSize,
            current: parseInt(params.currentPage, 10) || 1,
        },
    };

    return res.json(result);
}

function deleteByPrimaryKeyForProSystemAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProSystemAuth(req, res, u);
}


function selectByPrimaryKeyForProSystemAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProSystemAuth(req, res, u);
}



function insertForProSystemAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;



    const i = Math.ceil(Math.random() * 10000);
    tableListDataSource.unshift({
        system_id: `李海波 system_id ${i}`,
        auth_id: `李海波 auth_id ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });


    return getProSystemAuth(req, res, u);
}


function updateForProSystemAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.map(item => {
        if (item.id === id) {
        Object.assign(item, { desc, name });
        return item;
    }
    return item;
});


    return getProSystemAuth(req, res, u);
}


function deleteByMapForProSystemAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProSystemAuth(req, res, u);
}



function updateByMapForProSystemAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProSystemAuth(req, res, u);
}

function getOneForProSystemAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(undefined!=id)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProSystemAuth(req, res, u);
}

function getAllForProSystem(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProSystem(req, res, u);
}

function queryPageForProSystem(req, res, u, b) {


    return getProSystem(req, res, u);
}
function queryPageForProAuthority(req, res, u, b) {


    return getProSystem(req, res, u);
}

function countForProSystemAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProSystemAuth(req, res, u);
}


function insertBatchForProSystemAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProSystemAuth(req, res, u);
}



function deleteBatchForProSystemAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProSystemAuth(req, res, u);
}


function updateBatchForProSystemAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProSystemAuth(req, res, u);
}




export default {
    'GET /${webpath}/web/api/service/prosystemauth/delete': deleteByPrimaryKeyForProSystemAuth,
    'GET /${webpath}/web/api/service/prosystemauth/get': selectByPrimaryKeyForProSystemAuth,
    'POST /${webpath}/web/api/service/prosystemauth/insert': insertForProSystemAuth,
    'POST /${webpath}/web/api/service/prosystemauth/update': updateForProSystemAuth,
    'POST /${webpath}/web/api/service/prosystemauth/deleteByMap': deleteByMapForProSystemAuth,
    'POST /${webpath}/web/api/service/prosystemauth/updateByMap': updateByMapForProSystemAuth,
    'POST /${webpath}/web/api/service/prosystemauth/getOne': getOneForProSystemAuth,
    'POST /${webpath}/web/api/service/prosystemauth/getAll': getAllForProSystem,
    'POST /${webpath}/web/api/service/prosystemauth/queryPage': queryPageForProSystem,
    'POST /${webpath}/web/api/service/prosystemauth/count': countForProSystemAuth,
    'POST /${webpath}/web/api/service/prosystemauth/insertBatch': insertBatchForProSystemAuth,
    'POST /${webpath}/web/api/service/prosystemauth/deleteBatch': deleteBatchForProSystemAuth,
    'POST /${webpath}/web/api/service/prosystemauth/updateBatch': updateBatchForProSystemAuth,
    
    
    'POST /${webpath}/web/api/service/prosystemauth/getPageProSystemByAuthId': getPageProSystemByAuthId,
    'POST /${webpath}/web/api/service/proauthority/queryPage': queryPageForProAuthority,
    
    'POST /${webpath}/web/api/service/prosystem/queryPage': queryPageForProSystemChild,
    'POST /${webpath}/web/api/service/prosystemauth/getSelectedProSystemByAuthId': getSelectedProSystemByAuthId,
};
